package cn.qingyun.gis.modules.enterprise.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.enterprise.constant.enums.LayerEnum;
import cn.qingyun.gis.modules.enterprise.entity.LayerData;
import cn.qingyun.gis.modules.enterprise.entity.ZiguanCommunity;
import cn.qingyun.gis.modules.enterprise.entity.dto.LayerDto;
import cn.qingyun.gis.modules.enterprise.mapper.LayerDataMapper;
import cn.qingyun.gis.modules.enterprise.request.BuildingRequest;
import cn.qingyun.gis.modules.enterprise.response.LayerResponse;
import cn.qingyun.gis.modules.enterprise.service.ILayerDataService;
import cn.qingyun.gis.modules.enterprise.service.IZiguanCommunityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author skb
 * @since 2024-04-01
 */
@Service
@Slf4j
public class LayerDataServiceImpl extends ServiceImpl<LayerDataMapper, LayerData> implements ILayerDataService {
    @Autowired
    private IZiguanCommunityService ziguanCommunityService;
    @Autowired
    private LayerDataMapper layerDataMapper;

    @Override
    public List<ZiguanCommunity> getZiGuanList(String code) {
        QueryWrapper<ZiguanCommunity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZiguanCommunity::getGridCode,code);
        return ziguanCommunityService.list(queryWrapper);
    }

    @Override
    public List<LayerResponse> getLayerList(BuildingRequest buildingRequest) {
        log.info("开始获取图层数据");
        long startTime = System.currentTimeMillis();
        List<String> types = buildingRequest.getTypes();
        List<LayerResponse> list = new ArrayList<>();
        if (Objects.isNull(types)) {
            throw new CommonException("建筑类型不能为空");
        }

        for (String type : types) {
            LayerResponse layerResponse = new LayerResponse();
            List<LayerDto> layerDtoList = new ArrayList<>();
            String tableName = LayerEnum.getTableByType(type);
            if("8".equals(type)){
                layerDtoList = layerDataMapper.getStationList(buildingRequest.getCode());
            } else if ("9".equals(type)) {
                layerDtoList = layerDataMapper.getBoxList(buildingRequest.getCode());
            } else{
                layerDtoList = layerDataMapper.getCommunityAndVillageList(tableName, buildingRequest.getCode());
            }
            layerResponse.setCount(layerDataMapper.getCount(tableName, buildingRequest.getCode()));
            layerResponse.setType(type).setList(layerDtoList);
            list.add(layerResponse);
        }
        long endTime = System.currentTimeMillis();
        log.info("获取图层数据结束，耗时：{}",endTime-startTime);
        return list;
    }

}